/*
 * OREKIT-X
 * Copyright 2002-2008 CS Communication & Systemes
 * 
 * Parts of this software package have been licensed to CS
 * Communication & Systemes (CS) under one or more contributor license
 * agreements.  See the NOTICE file distributed with this work for
 * additional information.
 *  
 * This is an experimental copy of OREKIT from www.orekit.org.
 * Please use the original OREKIT from orekit.org for normal work
 * unrelated to this research project.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License.  You
 * may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */
package ore.propagation.events;

import ore.Frame;
import ore.Orbit;
import ore.SpacecraftState;
import ore.errors.OrekitException;

/** 
 * Detector of node crossing events.  This class finds equator
 * crossing events: ascending or descending node crossing events.
 * 
 * The default implementation behavior is to continue propagation at
 * descending node crossing and to stop propagation at ascending node
 * crossing. This can be changed by overriding the eventOccurred
 * method in a derived class.
 * 
 * Beware that node detection will fail for almost equatorial
 * orbits. If for example a node detector is used to trigger an {@link
 * ore.forces.maneuvers.ImpulseManeuver ImpulseManeuver} and the
 * maneuver turns the orbit plane to equator, then the detector may
 * completely fail just after the maneuver has been performed! 
 * 
 * @author Luc Maisonobe
 */
public class NodeDetector
    extends AbstractDetector
{
    /** 
     * Frame in which the equator is defined.
     */
    private final Frame frame;


    /** 
     * The orbit is used only to set an upper bound for the max check
     * interval to (period/3).
     * 
     * @param orbit Initial orbit
     * @param frame Frame in which the equator is defined (typical
     * values are {@link ore.frames.FramesFactory#getEME2000() J<sub>2000</sub>} or
     * {@link ore.frames.FramesFactory#getITRF2005() ITRF 2005})
     */
    public NodeDetector( Orbit orbit, Frame frame) {
        super((orbit.getKeplerianPeriod() / 3.0), 1.0e-10);
        this.frame  = frame;
    }


    /** 
     * @return Frame in which the equator is defined
     */
    public Frame getFrame() {
        return this.frame;
    }

    /** 
     * Handle a node crossing event and choose what to do next.
     * 
     * The default behavior is to continue propagation at descending
     * node crossing and to stop propagation at ascending node
     * crossing.  This behavior can be changed by overriding this
     * method in a derived class.
     * 
     * @param s The current state information : date, kinematics, attitude
     * 
     * @param increasing If true, the value of the switching function
     * increases when times increases around event (note that increase
     * is measured with respect to physical time, not with respect to
     * propagation which may go backward in time)
     * @return one of {@link #STOP}, {@link #RESET_STATE}, {@link #RESET_DERIVATIVES}
     * or {@link #CONTINUE}
     * 
     * @exception OrekitException Some specific error
     */
    public int eventOccurred(SpacecraftState s, boolean increasing)
        throws OrekitException
    {
        if (increasing)
            return STOP;
        else
            return CONTINUE;
    }
    /**
     * {@inheritDoc}
     */
    public double g(SpacecraftState s) throws OrekitException {
        return s.getPositionVelocity(frame).getPosition().getZ();
    }
}
